define(['jquery', 'bootstrap', 'backend', 'table', 'form','cookie'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'fas/zcdevice/index' + location.search,
                    add_url: 'fas/zcdevice/add',
                    edit_url: 'fas/zcdevice/edit',
                    del_url: 'fas/zcdevice/del',
                    multi_url: 'fas/zcdevice/multi',
                    import_url: 'fas/zcdevice/import',
                    table: 'fas_zcdevice',
                }
            });

            var table = $("#table");

            //buttons.push({
            //    name: 'op1',
            //    title: __('采购记录'),
            //    classname: 'btn btn-xs btn-warning btn-addtabs',
            //    icon: 'fa fa-eercast',
            //    url: 'fas/zcorder/index'
            //});
            var buttons = [];
            buttons.push({
                name: 'op1',
                title: __('调拨记录'),
                classname: 'btn btn-xs btn-info btn-addtabs',
                icon: 'fa fa-truck',
                url: 'fas/zcdiaobojl/index'
            });

            buttons.push({
                name: 'op2',
                title: __('保养记录'),
                classname: 'btn btn-xs btn-warning btn-addtabs',
                icon: 'fa fa-puzzle-piece',
                url: 'fas/byjl/index'
            });

            buttons.push({
                name: 'op3',
                title: __('报修记录'),
                classname: 'btn btn-xs btn-danger btn-addtabs',
                icon: 'fa fa-sign-language',
                url: 'fas/bxjl/index'
            });

            buttons.push({
                name: 'op4',
                title: __('运停统计'),
                classname: 'btn btn-xs btn-info btn-addtabs',
                icon: 'fa fa-eercast',
                url: 'fas/zcdevicelog/tongji'
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search:false,
                showToggle: false,
                showExport: false,
                searchFormVisible: true,
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        //{checkbox: true},
                        //{field: 'id', title: __('Id'), operate:false},
                        //{field: 'zctype_id', title: __('Zctype_id')},
                        //{field: 'zcorder_id', title: __('Zcorder_id')},
                        {
                            field: 'operate',
                            width: "130px",
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: buttons,
                            formatter: Table.api.formatter.operate
                        },
                        {field: 'name', title: __('Name'), operate: 'LIKE %...%', placeholder:__('模糊搜索，*表示任意字符')},
                        {field: 'zctype.name', title: __('Zctype.name')},
                        {field: 'zctype.xh', title: __('Zctype.xh'), operate:false},
                        {field: 'fzr', title: __('Fzr'), operate: 'LIKE %...%', placeholder:__('模糊搜索，*表示任意字符')},
                        {field: 'fgroup', title: __('Fgroup'), operate: 'LIKE %...%', placeholder:__('模糊搜索，*表示任意字符')},
                        {field: 'ffactory', title: __('Ffactory'), operate: 'LIKE %...%', placeholder:__('模糊搜索，*表示任意字符')},
                        {field: 'sydw', title: __('Sydw'), operate: 'LIKE %...%', placeholder:__('模糊搜索，*表示任意字符')},
                        {field: 'sydd', title: __('Sydd'), operate: 'LIKE %...%', placeholder:__('模糊搜索，*表示任意字符')},
                        {field: 'zctype.type', title: __('Zctype.type'), searchList: {"成型设备":__('成型设备'),"线外加工设备":__('线外加工设备'),"组底设备":__('组底设备'),"裁断设备":__('裁断设备'),"针车设备":__('针车设备'),"其他设备":__('其他设备')}},
                        {field: 'zctype.zclh', title: __('Zctype.zclh'), operate:false},
                        {field: 'qydate', title: __('Qydate'), operate:false},
                        {field: 'sbsynx', title: __('Sbsynx'), operate:false},
                        {field: 'sbjz', title: __('Sbjz'), operate:false},
                        {field: 'cz', title: __('Cz'), operate:false},
                        {field: 'images', title: __('Images'), events: Table.api.events.image, formatter: Table.api.formatter.images, operate:false},
                        //{field: 'attachfiles', title: __('Attachfiles'), formatter:Table.api.formatter.url, operate:false}, //1个附件可以查看，多个的不可以
                        //{field: 'attachfiles', title: __('Attachfiles'),operate:false},
                        //{field: 'bfdate', title: __('Bfdate'), operate:false},
                        {field: 'state', title: __('State'), defaultValue:"0", searchList: {"0":__('State 0'),"1":__('State 1'),"2":__('State 2'),"3":__('State 3'),"4":__('State 4'),"5":__('State 5')}, formatter: Table.api.formatter.normal},
                        //{field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'zctype.id', title: __('Zctype.id')},

                        //{field: 'zctype.create_time', title: __('Zctype.create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'zctype.update_time', title: __('Zctype.update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'zcorder.id', title: __('Zcorder.id')},
                        //{field: 'zcorder.zctype_id', title: __('Zcorder.zctype_id')},
                        //{field: 'zcorder.po', title: __('Zcorder.po')},
                        //{field: 'zcorder.gys', title: __('Zcorder.gys')},
                        //{field: 'zcorder.sqdw', title: __('Zcorder.sqdw')},
                        //{field: 'zcorder.sydd', title: __('Zcorder.sydd')},
                        //{field: 'zcorder.num', title: __('Zcorder.num')},
                        //{field: 'zcorder.state', title: __('Zcorder.state')},
                        //{field: 'zcorder.otype', title: __('Zcorder.otype')},
                        //{field: 'zcorder.rukudate', title: __('Zcorder.rukudate'), operate:'RANGE', addclass:'datetimerange'},
                        //{field: 'zcorder.ysdate', title: __('Zcorder.ysdate'), operate:'RANGE', addclass:'datetimerange'},
                        //{field: 'zcorder.gsdate', title: __('Zcorder.gsdate'), operate:'RANGE', addclass:'datetimerange'},
                        //{field: 'zcorder.ysmanager', title: __('Zcorder.ysmanager')},
                        //{field: 'zcorder.create_time', title: __('Zcorder.create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'zcorder.update_time', title: __('Zcorder.update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},

                    ]
                ]
            });

            // 选择隐藏字段事件，使用cookie存入
            table.on('column-switch.bs.table', function (e, json) {
                    var myColumns=[];
                    $.each(table.bootstrapTable('getHiddenColumns'), function(i, item) {
                         myColumns.push(item.field);
                    });
                    $.cookie('fa_fas_zcdevice', JSON.stringify(myColumns), { expires: 365, path: '/'});
             });
             if($.cookie('fa_fas_zcdevice')){
                //读取cookie隐藏字段
                $.each(JSON.parse($.cookie('fa_fas_zcdevice')), function(i, item) {
                  table.bootstrapTable('hideColumn', item);
                });
             }

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});